/******************************************************************************
 * The contents of this file are subject to the   JDI License  Version 1.0
 * ("License"); You may not use this file except in compliance with the License
 * Software distributed under the License is distributed on an  "AS IS"  basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 * The Initial Developer of the Original Code is 陈慧然. Portions created by 
 * 陈慧然 are Copyright 陈慧然.
 * All Rights Reserved.
 *
 *              Contact john_holen@163.com for licensing alternatives.
 *
 *****************************************************************************/


package org.peony.client.web.components.form;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.peony.client.web.components.WebComponents;
import org.peony.exceptions.RenderException;
import org.peony.standard.unvarying.client.ComponentsSize;
import org.peony.tools.StringUtil;

/**
 *  
 *
 *  @author     陈慧然
 *  @version    Id: Select.java, v 0.000 2008-5-5 上午11:09:44 陈慧然 Exp
 */
public class Select extends WebComponents{
	private List<SelectOption> options = new ArrayList<SelectOption>();
	private Label label;
	private boolean isDisabled = false;
	private boolean isMultiple = false;
	private int size = 1;
	public List<SelectOption> getOptions() {
		return options;
	}
	public void setOptions(List<SelectOption> options) {
		this.options = options;
	}
	public void SetAOption(SelectOption option){
		options.add(option);
	}
	public void SetAOption(SelectOption option, int index){
		options.add(index, option);
	}
	public Label getLabel() {
		return label;
	}
	public void setLabel(Label label) {
		this.label = label;
	}
	public boolean isDisabled() {
		return isDisabled;
	}
	public void setDisabled(boolean isDisabled) {
		this.isDisabled = isDisabled;
	}
	public boolean isMultiple() {
		return isMultiple;
	}
	public void setMultiple(boolean isMultiple) {
		this.isMultiple = isMultiple;
	}
	public int getSize() {
		return size;
	}
	public void setSize(int size) {
		this.size = size;
	}
	@Override
	public String toStandardString() throws RenderException {
		if(StringUtil.isEmpty(getId()))
			throw new RenderException("Select (A Component) Can NOT has no id!");
		StringBuffer sb = new StringBuffer();
		sb.append(getLabel()==null?empty:getLabel().toStandardString())
			.append("<select")
			.append(" id= \"").append(getId()).append("\"")
			.append(" name=\"").append(getId()).append("\"")
			.append(StringUtil.isNotEmpty(getUIClass())?" class=\"" + getUIClass()+"\"":empty)
			.append(isDisabled()?" disabled=\"disabled\"":empty)
			.append(isMultiple()?" multiple=\"multiple\"":empty)
			.append(" size=\"").append(getSize()<1?ComponentsSize.sizeForSelect:getSize()).append("\">");
		List<SelectOption> list = getOptions();
		if(list!=null && list.size()>0)
			for (Iterator<SelectOption> iterator = list.iterator(); iterator.hasNext();) {
				SelectOption option = iterator.next();
				sb.append(option.toStandardString());
			}
		sb.append("</select>");
		return sb.toString();
	}
}
